//
//  YQSearchBaseBar.m
//  Pension
//
//  Created by tbb on 2019/6/4.
//  Copyright © 2019  YIQI. All rights reserved.
//

#import "YQSearchBaseBar.h"

#import <objc/runtime.h>

@interface YQSearchBaseBar ()<UISearchBarDelegate>

@end

@implementation YQSearchBaseBar
-(void)setPlaceholderStr:(NSString *)placeholderStr {
    _placeholderStr = [placeholderStr copy];
    self.searchBar.placeholder = placeholderStr;
}
-(void)setSearchKey:(NSString *)searchKey {
    _searchKey = [searchKey copy];
    self.searchBar.text = searchKey;
}
-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.searchBar];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addSubview:self.searchBar];
    }
    return self;
}


#pragma --mark lazy
-(UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] initWithFrame:self.bounds];
        _searchBar.tintColor = [UIColor colorWithHexString:@"#FA969D"];
        
        _searchBar.layer.cornerRadius = self.height/2.0;
        _searchBar.layer.masksToBounds = YES;
        //设置背景图是为了去掉上下黑线
        _searchBar.backgroundImage = [[UIImage alloc] init];
        _searchBar.backgroundColor = HexColor(0xF4F4F4);//[UIColor colorWithHexString:@"#F3F5F9" alpha:0.1];
        _searchBar.barTintColor = HexColor(0x4A99F5);//[UIColor colorWithHexString:@"#4A99F5" alpha:0.1];
        // 修改cancel
        _searchBar.showsCancelButton = NO;
        _searchBar.barStyle = UIBarStyleDefault;
        _searchBar.delegate = self;
        // 修改cancel
        _searchBar.showsSearchResultsButton = NO;

        
        
    
        UITextField *searchField = [_searchBar valueForKey:@"searchField"];
        if(searchField) {
            [searchField setBackgroundColor:[UIColor clearColor]];
            [searchField setValue:HexColor(0xA0A0A0) forKeyPath:@"placeholderLabel.textColor"];
        }
        searchField.font = [UIFont systemFontOfSize:15];
        //_placeholderLabel iOS 去掉 _
        [searchField setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"placeholderLabel.font"];
        
        
    }
    return _searchBar;
}
#pragma --mark lazy end
-(void)setResignFirst:(BOOL)resignFirst {
    _resignFirst = resignFirst;
    if (resignFirst) {
        [self.searchBar resignFirstResponder];
    }
}
#pragma --mark UISearchBarDelegateDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    if ([self.delegate respondsToSelector:@selector(yq_searchBarShouldBeginEditing:)]) {
        return [self.delegate yq_searchBarShouldBeginEditing:searchBar];
    }
    return YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    if ([self.delegate respondsToSelector:@selector(yq_searchBarSearchButtonClicked:)]) {
        [self.delegate yq_searchBarSearchButtonClicked:searchBar];
    }
    self.resignFirst = YES;
}

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSString *title = [searchBar.text stringByReplacingCharactersInRange:range withString:text];
    
    if (title.length > 30) {
        return NO;
    }
    
    if ([self.delegate respondsToSelector:@selector(yq_shouldChangeText:)]) {
        [self.delegate yq_shouldChangeText:title];
    }
    
    return YES;
}
#pragma --mark UISearchBarDelegateDelegate end

+ (void)load
{
    // hook：钩子函数
    Method method1 = class_getInstanceMethod([UISearchBar class], @selector(textFieldShouldClear:));
    Method method2 = class_getInstanceMethod(self, @selector(tbb_textFieldShouldClear:));
    method_exchangeImplementations(method1, method2);
}

-(void)tbb_textFieldShouldClear:(UITextField *)textField {
    //self为局部变量，这里self为searchBar
    YQSearchBaseBar *superview = (YQSearchBaseBar *)self.superview;
//    superview.searchBar 
    if ([superview.delegate respondsToSelector:@selector(yq_textFieldShouldClear:)]) {
        [superview.delegate yq_textFieldShouldClear:textField];
    }
    
}


@end
